package managers;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import conn.Conn;
import business.Module;

public class MngModules extends Manager {
	
	public MngModules() {

	}
	
	public ArrayList<Module> getModules(byte userID) {
		ArrayList<Module> modules = new ArrayList<Module>();
		
		try {
			String	sql = "SELECT DISTINCT mdl.id, mdl.module " +
						  "FROM modules mdl " +
						  "INNER JOIN programs prg ON mdl.id = prg.module " +
						  "INNER JOIN access acc ON prg.id = acc.program " + 
						  "INNER JOIN profiles prf ON acc.profile = prf.id " +
						  "INNER JOIN users usr ON prf.id = usr.profile " +
						  "WHERE usr.id = " + userID + " " +
						  "ORDER BY mdl.order";
				
			Statement stm = Conn.getConn().createStatement();
			rst = stm.executeQuery(sql);
				
			while (rst.next()) {
				Module mod = new Module();
				mod.setId(rst.getByte("id"));
				mod.setModule(rst.getString("module"));
				
				MngPrograms mngPrgs = new MngPrograms();
				mod.setPrograms(mngPrgs.getPrograms(userID, mod.getId()));
				
				modules.add(mod);
			}	
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return modules;
	}
}
